// Testing program for dama_logic

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "dama_logic.c"
#include "dama_data.h"

void forgeProposal(move_message_t *p, char playerMoving, int startX, int startY, int endX, int endY, int flag);
void createBoard(match_t *m);
int even(int n);
void printBoard(match_t *m);

int main()
{
	int result, result1;
	
	match_t *m = malloc(sizeof(match_t));
	
	m->state = InProgress;
    m->lastmoveX = 0;
    m->lastmoveY = 0;
    m->wpieces = 12;
    m->bpieces = 12;
	
	move_message_t *proposal = malloc(sizeof(move_message_t));
	
	createBoard(m);
	printBoard(m);
	
	// 1 Mossa semplice	
	forgeProposal(proposal, 'w', 2, 2, 3, 3, 0);
	result = evaluateProposal(proposal, m);
	(result == 0)? printf("1.1: mossa semplice ok\n"): printf("1.1: fallisce test mossa semplice\n");
	printBoard(m);
	forgeProposal(proposal, 'b', 5, 5, 4, 6, 0);
	printf("Da %c a %c.\n", m->board[5][5], m->board[4][6]);
	result1 = canMove(proposal, 0, m->board);
	(result1 == -1)? printf("1.2.1: mossa semplice ok\n"): printf("1.2.1: fallisce test mossa semplice\n");
	result = evaluateProposal(proposal, m);
	(result == 0)? printf("1.2: mossa semplice ok\n"): printf("1.2: fallisce test mossa semplice\n");
	printBoard(m);
	printf("Da %c a %c.\n", m->board[5][5], m->board[4][6]);
	
	printf("\n\n\n\n\n");
	
	// mossa aggiuntiva
	forgeProposal(proposal, 'b', 5, 3, 4, 4, 0);
	evaluateProposal(proposal, m);
	
	// 2 Mangiare
	forgeProposal(proposal, 'w', 3, 3, 5, 5, 0);
	result1 = mustEat('w', 4, 4, m->board);
	(result1 > 0)? printf("2.0.1: avverto eat ok\n"): printf("2.0.1: fallisce avverto eat\n");
	evaluateProposal(proposal, m);
	printBoard(m);
	forgeProposal(proposal, 'b', 6, 6, 4, 4, 0);
	result1 = mustEat('b', 4, 4, m->board);
	(result1 > 0)? printf("2.0.1: avverto eat ok\n"): printf("2.0.1: fallisce avverto eat\n");
	evaluateProposal(proposal, m);
	printBoard(m);
	
	// vado avanti
	forgeProposal(proposal, 'w', 2, 4, 3, 3, 0);
	evaluateProposal(proposal, m);
	printBoard(m);
	
	forgeProposal(proposal, 'b', 4, 4, 2, 2, 0);
	evaluateProposal(proposal, m);
	printBoard(m);
	
	forgeProposal(proposal, 'w', 1, 1, 3, 3, 0);
	evaluateProposal(proposal, m);
	printBoard(m);
	
	forgeProposal(proposal, 'b', 7, 7, 6, 6, 0);
	evaluateProposal(proposal, m);
	printBoard(m);
	
	forgeProposal(proposal, 'w', 3, 3, 4, 4, 0);
	evaluateProposal(proposal, m);
	printBoard(m);
	
	forgeProposal(proposal, 'b', 6, 6, 5, 5, 0);
	evaluateProposal(proposal, m);
	printBoard(m);
	
	forgeProposal(proposal, 'w', 4, 4, 6, 6, 0);
	evaluateProposal(proposal, m);
	printBoard(m);
	
	forgeProposal(proposal, 'b', 5, 1, 4, 2, 0);
	evaluateProposal(proposal, m);
	printBoard(m);
	
	// Segnalo dama
	forgeProposal(proposal, 'w', 6, 6, 7, 7, 0);
	evaluateProposal(proposal, m);
	printBoard(m);
	
	// vado avanti
	forgeProposal(proposal, 'b', 6, 4, 5, 3, 0);
	evaluateProposal(proposal, m);
	printBoard(m);
	
	// Muovo dama
	forgeProposal(proposal, 'W', 7, 7, 6, 6, 0);
	evaluateProposal(proposal, m);
	printBoard(m);
	
	// vado avanti
	forgeProposal(proposal, 'b', 4, 2, 3, 1, 0);
	//result1 = mustEat('w', 7, 7, m->board);
	//(result1 == 0)? printf("2.0.1: avverto eat ok\n"): printf("2.0.1: fallisce avverto eat\n");
	evaluateProposal(proposal, m);
	printBoard(m);
	
	// Mangio multiplo
	forgeProposal(proposal, 'w', 2, 0, 4, 2, 0);
	evaluateProposal(proposal, m);
	printf("flag %d\n", proposal->flag);
	printBoard(m);
	
	// TODO: FINIRE LOGIC: MANGIARE TRA DAME
	
	
	
}

void forgeProposal(move_message_t *p, char playerMoving, int startX, int startY, int endX, int endY, int flag)
{
	p->playerMoving = playerMoving;
	p->startX = startX;
	p->startY = startY;
	p->endX = endX;
	p->endY = endY;
	p->flag = flag;

}

void createBoard(match_t *m)
{	

	int q;
	for(int j=0; j<=2; j++){
		for(int i=0; i<8; i++)
		{
			q=i+j;
			if(even(q) == 1)
			{
				m->board[j][i]='w';
				m->wpieces++;
			}else{
				m->board[j][i]='e';
			}
		}
	}
	
	for(int j=3; j<=4; j++){
		for(int i=0; i<8; i++)
		{
			m->board[j][i]='e';
		}
	}
	
	for(int j=5; j<=7; j++){
		for(int i=0; i<8; i++)
		{
			q=i+j;
			if(even(q) == 1)
			{
				m->board[j][i]='b';
				m->bpieces++;
			}else{
				m->board[j][i]='e';
			}
		}
	}
}

int even(int n)
{
	if ((n % 2)==0) {
		return 1;
	} else {
		return 0;
	}
}

void printBoard(match_t *m)
{
	for(int i=0; i<8; i++)
	{
		for(int j=0; j<7; j++)
		{
			printf("[%c]", m->board[i][j]);
		}
		printf("[%c]\n", m->board[i][7]);
	}
}
